package com.jdls.day16.work;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 嘉德罗斯
 * @date 2021/8/7
 */
public class Test1 {
    public static int a = 100;
    public static void main(String[] args) {
        //1. 用lock锁解决线程安全
        ReentrantLock lock = new ReentrantLock();
        new Thread(()->{
            while (true){
                lock.lock();
                if (a <= 0) {
                    lock.unlock();
                    break;
                }else {
                    System.out.println(a--);
                    lock.unlock();
                }
            }
        }).start();
        new Thread(()->{
            while (true){
                lock.lock();
                if (a <= 0) {
                    lock.unlock();
                    break;
                }else {
                    System.out.println(a++);
                    lock.unlock();
                }
            }
        }).start();
    }
}
